package cn.interestinghop.servlet;

import cn.interestinghop.model.entity.User;
import cn.interestinghop.service.user.UserService;
import cn.interestinghop.service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PipedWriter;
import java.io.PrintWriter;

@WebServlet(name = "AddServlet",urlPatterns = "/AddServlet")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        String l_user = request.getParameter("l_user");        //用户名
        String nick_name = request.getParameter("nick_name");  //昵称
        String l_pwd = request.getParameter("l_pwd");          //密码
        String pwd = request.getParameter("pwd");              //确认密码
        Integer sex = Integer.valueOf(request.getParameter("sex"));//性别
        String l_email = request.getParameter("l_email");      //邮箱
        String l_tel = request.getParameter("l_tel");          //手机

        User user = new User();
        user.setAccount(l_user);
        user.setNick_name(nick_name);
        user.setPassword(l_pwd);
        user.setGender(sex);
        user.setEmail(l_email);
        user.setPhone(l_tel);

        UserService service = new UserServiceImpl();

        int add = service.add(user);

        PrintWriter out = response.getWriter();
        if(add == 1){
            out.print("<script>alert('注册成功');window.location.href='/client/Index.jsp';</script>");
        }else{
            out.print("<script>alert('注册失败');window.location.href='/client/Regist.jsp';</script>");
        }
    }
}
